import api, { APIResponse } from './api'
import { Script, CreateScriptData, UpdateScriptData, ScriptSearchParams, ScriptStats } from '@/types/script'

// 获取脚本列表
export const getScripts = (params?: ScriptSearchParams): Promise<APIResponse<{
  scripts: Script[]
  total: number
}>> => {
  return api.get('/scripts', { params }).then(res => res.data)
}

// 获取单个脚本
export const getScript = (id: string): Promise<APIResponse<Script>> => {
  return api.get(`/scripts/${id}`).then(res => res.data)
}

// 创建脚本
export const createScript = (data: CreateScriptData): Promise<APIResponse<Script>> => {
  return api.post('/scripts', data).then(res => res.data)
}

// 更新脚本
export const updateScript = (id: string, data: UpdateScriptData): Promise<APIResponse<Script>> => {
  return api.put(`/scripts/${id}`, data).then(res => res.data)
}

// 删除脚本
export const deleteScript = (id: string): Promise<APIResponse<void>> => {
  return api.delete(`/scripts/${id}`).then(res => res.data)
}

// 移动脚本
export const moveScript = (id: string, folderId?: string): Promise<APIResponse<Script>> => {
  return api.patch(`/scripts/${id}/move`, { folder_id: folderId }).then(res => res.data)
}

// 复制脚本
export const copyScript = (id: string, data: {
  name?: string
  folder_id?: string
  created_by?: string
}): Promise<APIResponse<Script>> => {
  return api.post(`/scripts/${id}/copy`, data).then(res => res.data)
}

// 更新脚本标签
export const updateScriptTags = (id: string, tags: string[]): Promise<APIResponse<void>> => {
  return api.patch(`/scripts/${id}/tags`, { tags }).then(res => res.data)
}

// 获取所有标签
export const getAllTags = (): Promise<APIResponse<string[]>> => {
  return api.get('/scripts/tags').then(res => res.data)
}

// 批量操作脚本
export const batchOperateScripts = (data: {
  script_ids: string[]
  operation: 'delete' | 'move' | 'update_tags'
  data?: any
}): Promise<APIResponse<{
  success_count: number
  failed_count: number
  errors: string[]
}>> => {
  return api.post('/scripts/batch', data).then(res => res.data)
}

// 导入脚本
export const importScripts = (files: FileList, data: {
  folder_id?: string
  created_by?: string
}): Promise<APIResponse<{
  success_count: number
  failed_count: number
  errors: string[]
}>> => {
  const formData = new FormData()
  Array.from(files).forEach(file => {
    formData.append('files', file)
  })
  if (data.folder_id) formData.append('folder_id', data.folder_id)
  if (data.created_by) formData.append('created_by', data.created_by)
  
  return api.post('/scripts/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  }).then(res => res.data)
}

// 导出脚本
export const exportScripts = (scriptIds: string[], format = 'zip'): Promise<Blob> => {
  return api.post('/scripts/export', {
    script_ids: scriptIds,
    format,
  }, {
    responseType: 'blob',
  }).then(res => res.data)
}

// 获取脚本统计
export const getScriptStats = (userId?: string): Promise<APIResponse<ScriptStats>> => {
  return api.get('/scripts/stats/overview', {
    params: { user_id: userId }
  }).then(res => res.data)
}